fix: Android recorderController.stop() never completes#486
Open
rubenvde wants to merge 2 commits into
Open
Conversation
Prevent encoder hang and ensure proper cleanup when stopping recordings. Add queueEosBuffer helper and proactively queue EOS in signalToStop if an input buffer is available, run MediaCodec callback on the handler, move completionCallback to finally, call release() after sending results or on error, and post result.success on the main looper for thread-safety.
Post the EOS check to the handler thread so EOS queuing runs on the same thread as onInputBufferAvailable, preventing race conditions when an input buffer is already available. Add an early return if the encoder is stopped. Move handlerThread.quitSafely() into the finally block and remove join() to avoid deadlocking when stopEncoder is invoked from callbacks; ensure completionCallback is invoked before quitting the thread.
Contributor
|
Same issue! |
|
This does not work for me it actually crashes the app when I call stop(). |
sherief4
approved these changes
Apr 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes a hang where
await recorderController.stop()on Android never completes, especially for recordings >30 seconds. This was introduced in the 2.0.x rewrite that replacedMediaRecorderwithAudioRecord+MediaCodec.Root causes fixed:
Deadlock in
stopEncoder()—stopEncoder()is called fromonOutputBufferAvailableon the handler thread, but it calledhandlerThread.join(), waiting for itself to finish. Removed thejoin()anduse
quitSafely()only.Race condition in
signalToStop()— EOS buffer queuing now runs on the handler thread viahandler.post {}, avoiding races withonInputBufferAvailablethat caused "MediaCodec discarded an unknownbuffer" errors.
release()called before async completion — For the non-WAV path,release()was called synchronously aftersignalToStop(), before the completion callback fired. Movedrelease()into the completioncallback.
result.success()called from wrong thread —sendRecordingResult()now posts to the main looper, since Flutter'sMethodChannel.Resultmust be called on the platform thread.completionCallbackswallowed on error — MovedcompletionCallback?.invoke()to afinallyblock instopEncoder()so the Flutter result is always resolved.Missing handler in
setCallback()—mediaCodec.setCallback()now passes thehandlerso callbacks run on the intendedHandlerThread.Checklist
fix:,feat:,docs:etc).docsand added dartdoc comments with///.examplesordocs.There's no test infrastructure in the project and the bug is a native Android threading issue that's difficult to unit test from Dart
Breaking Change?
Related Issues
#470